﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.Business
{
    public class ManagerSquare
    {
        public static List<string> GetAllSquares()
        {
            return DataAccessSquare.GetAllSquares();
        }

        public static ICollection<Square> GetSquares()
        {
            return DataAccessSquare.GetSquares();
        }

        public static void NewSquare(Square newSquare)
        {
            DataAccessSquare.InsertNewSquare(newSquare);
        }

        public static void VerifyIfSquareDataIsCorrect(Square mp)
        {
            if (GetSquareById(mp) != null)
                throw new Exception("El Manzano que ingreso ya existe porfavor ingrese otro.");
            if (String.IsNullOrWhiteSpace(mp.SquareId))
                throw new Exception("El Manzano debe contener al menos un caracter.");
        }

        public static Square GetSquareById(Square mp)
        {
            List<Square> squares = DataAccessSquare.SearchSquareById(mp);
            if (squares.Count == 0)
                return null;
                var temp = squares.First();
            return squares.First();
        }

        public static ICollection<Square> GetSquareByRelativeId(Square k)
        {
            ICollection<Square> km = DataAccessSquare.SearchSquaresByRelativeId(k);
            if (km.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            return km;
        }

        public static string GetUbicationImage(Square square)
        {
            string u =DataAccessSquare.GetUbicationImageSquare(square);
            if (DataAccessSquare.ExistImage(u))
                return u;
            else
                return null;
        }

        public static List<Square> GetAllSquaresWithAllotmentsNotAssignedAndEnables()
        {
            return DataAccessSquare.SearchSquaresWithAllotmentsNotAssignedAndIsActive().Where(
                x=>x.Allotments.Any(y => y.KardexId==null)
                ).ToList();
        }


        public static ICollection<Square> GetSquareByRelativeIdAndState(Square k, string state)
        {
            ICollection<Square> s;
            switch (state)
            {
                case ("Activo"):

                    s = DataAccessSquare.SearchSquareByParameterEnable(k);
                    break;
                case ("Inactivo"):

                    s = DataAccessSquare.SearchSquareByParameterDisable(k);
                    break;
                default:
                    s = DataAccessSquare.SearchSquareByParameterAll(k);
                    break;
            }
            if (s.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            return s;

        }

        public static void DisableSquare(Square square)
        {
            ICollection<Allotment> s = ManagerAllotment.GetAllotmentsOcupedInSquare(square);
            if(s.Count!=0)
                throw new Exception("El Manzano que desea inhabilitar tiene lotes ocupados. Para inhabilitar un manzano no debe tener lotes ocupados.");
            Square sq = GetSquareById(square);
            DataAccessSquare.ChangeState(sq);
        }

        public static void EnableSquare(Square square)
        {
            Square sq = GetSquareById(square);
            DataAccessSquare.ChangeState(sq);
        }

        public static void UpdateSquare(Square modifySquare)
        {
            DataAccessSquare.UpdateSquare(modifySquare);
        }

        public static void UpdateImage(string ubicationCroquisSquare, Square modifySquare)
        {
            string u=GetUbicationImage(modifySquare);
            if (u != null)
                DeleteImage(u);
            SaveImage(ubicationCroquisSquare, modifySquare);
        }

        private static void DeleteImage(string ubicationCroquisSquare)
        {

            //StreamReader sr = new StreamReader(ubicationCroquisSquare);
            //sr.Close();
            //sr.Dispose();
            //var myFile = File.(ubicationCroquisSquare);
            //myFile.Close();
            File.Delete(ubicationCroquisSquare);
        }

        public static void SaveImage(string ubicationCroquisSquare, Model.Square s)
        {
            if (!String.IsNullOrWhiteSpace(ubicationCroquisSquare))
                if (DataAccessSquare.ExistImage(ubicationCroquisSquare))
                    DataAccessSquare.CopyImageToLocalFolder(ubicationCroquisSquare, s);
        }

        public static ICollection<string> GetSquareNotAssignedWithAssignedToKardex(Kardex kardex)
        {
            return DataAccessSquare.GetAllSquaresWithKardexOrAvailable(kardex);
        }
    }
}
